package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.WorkOutsideRecordMapper;
import com.zhijian.medical.entity.po.WorkOutsideRecord;
import com.zhijian.medical.entity.vo.request.WorkOutsideRecordSearchVo;
import com.zhijian.medical.entity.vo.response.WorkOutsideRecordVo;
import com.zhijian.medical.service.WorkOutsideRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 外勤业务处理层
 * 
 * @author HCG
 * @version: 1.0
 * @date 2022-01-18 15:27:01
 */
@Slf4j
@Service
public class WorkOutsideRecordServiceImpl implements WorkOutsideRecordService {

	@Autowired
    private WorkOutsideRecordMapper mapper;

    @Override
    public WorkOutsideRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public WorkOutsideRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(WorkOutsideRecord workOutsideRecord) {
        Date now = new Date();
        workOutsideRecord.setCreateTime(now);
        workOutsideRecord.setUpdateTime(now);
        return mapper.insert(workOutsideRecord) > 0 ? workOutsideRecord.getId() : 0L;
    }

    @Override
    public boolean update(WorkOutsideRecord workOutsideRecord) {
        Date now = new Date();
        workOutsideRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(workOutsideRecord) != 0;
    }

    @Override
    public Integer count(WorkOutsideRecordSearchVo workOutsideRecordSearchVo) {
        return mapper.count(workOutsideRecordSearchVo);
    }

    @Override
    public List<WorkOutsideRecordVo> selectList(WorkOutsideRecordSearchVo workOutsideRecordSearchVo) {
        return mapper.selectList(workOutsideRecordSearchVo);
    }

    @Override
    public Page<WorkOutsideRecordVo> selectPage(WorkOutsideRecordSearchVo workOutsideRecordSearchVo) {
        //先直接查总数
        int count = count(workOutsideRecordSearchVo);
        //分页
        Page<WorkOutsideRecordVo> page = new Page<>(true, count, workOutsideRecordSearchVo.getPageNum(), workOutsideRecordSearchVo.getPageSize());
        workOutsideRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<WorkOutsideRecordVo> responseVos = this.selectList(workOutsideRecordSearchVo);
        page.setData(responseVos);
        return page;
    }
    
}